package com.tuanzi.loan.web.bean.family;

import static com.tuanzi.loan.business.constants.DictionaryCode.ADVERT_POSITION;
import static com.tuanzi.loan.business.constants.DictionaryCode.RECOMMEND_POSITION;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.common.CommProductChannel;
import com.tuanzi.loan.business.entity.family.ApiActivity;
import com.tuanzi.loan.business.entity.family.ContentActivity;
import com.tuanzi.loan.business.service.family.ProductMatchRuleService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.view.family.ProductMatchRuleView;
import com.tuanzi.loan.web.configuration.JSFInfoBean;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author Ray 2017/09/21
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class MatchRuleInfoBean extends JSFInfoBean<ProductMatchRuleView> {

	private static final long serialVersionUID = -2152778554108785342L;

	@Autowired
	private ProductMatchRuleService productMatchRuleService;

	private List<ContentActivity> contentActivities;
	private List<ApiActivity> apiActivities;
	private List<CommProductChannel> infoFlowChannels;
	private List<CommProductChannel> industryChannels;
	private List<CommProductChannel> applicationDistributionChannels;
	private List<CommProductChannel> smsChannels;
	private List<CommProductChannel> otherChannels;

	@PostConstruct
	private void init() {
		String id = getParamFromRequest("id");
		if (StringUtils.isNotBlank(id)) {
			$ = productMatchRuleService.loadProductMatchRuleView(NumberUtils.toLong(id));
		} else {
			$ = new ProductMatchRuleView();
			$.setPriority(new ProductMatchRuleView.ProductMatchRulePriority());
			$.setItem(new ProductMatchRuleView.ProductMatchRuleItem());
			$.setConfig(new ProductMatchRuleView.ProductMatchRuleConfig());
			changeStatusToAdd();
		}
		Map<Integer/* channel group */, List<CommProductChannel>> groupedChannels = getCommProductChannels().parallelStream()
				.collect(Collectors.groupingBy(CommProductChannel::getChannelGroup));

		infoFlowChannels = groupedChannels.get(0);
		industryChannels = groupedChannels.get(1);
		applicationDistributionChannels = groupedChannels.get(2);
		smsChannels = groupedChannels.get(3);
		otherChannels = groupedChannels.get(4);

	}

	public void onSave() {
		productMatchRuleService.saveOrUpdateProductMatchRuleView($);
		changeStatusToView();
		addSuccessfullyMessage();
	}

	@Override
	protected List<String> getDictionaryCodes() {
		return Lists.newArrayList(RECOMMEND_POSITION, ADVERT_POSITION);
	}

}
